libname blue "/.../data"; 


/**********************************************************************
* Author : Dhiren Patki
*
* This program prepares a file to compute the fraction of firm-years
* lost to the multi-plan firm restriction: i.e., firms may only sponsor
* 1 DB plan or, if they sponsor multiple plans, they must freeze/convert
* them at the same time. 
* The input files used in this program are constructed in 02_5500BRB.sas
**********************************************************************/

/*Sum the active participants at the firmid-year level*/

proc sort data = blue.db_brx_panel;
	by firmid plan_year_end;
run;

proc means noprint data = blue.db_brx_panel;
	by firmid plan_year_end;
	output out = actv_ptcp_firm
	sum(adj_db_partcp) = total_active_ptcp;
run;

proc sql;
	create table db_brx_panel_1 as
	select a.*, b.total_active_ptcp
	from blue.db_brx_panel as a inner join
	actv_ptcp_firm as b
	on a.firmid=b.firmid & a.plan_year_end = b.plan_year_end;
quit;

/*Keep just one observation per firm-year*/
proc sort data = db_brx_panel_1 out = temp nodupkey;
	by firmid plan_year_end;
run;

data temp;
	set temp;
	lost_to_multiplan=/*redacted*/;
	if in_sample=1 & max_db_cnt = min_db_cnt then lost_to_multiplan=/*redacted*/;
run;

proc sort data = temp;
	by lost_to_multiplan;
run;

/*Output a separate file for concentation ratio calculations*/
proc contents data = temp;
run;

data temp2 (keep = firmid lbd_emp lbd_pay esize_br epayroll_br total_active_ptcp plan_year_end lost_to_multiplan
            rename= (plan_year_end=year));
      set temp;
run;

proc export data = temp2
	outfile = "/.../data/multiplan_cratio.csv"
	dbms = csv replace;
run;

